﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using System.ServiceModel.Description;
using contract1;
using services;
namespace wsn
{
    class APIHost
    {
        ServiceHost host;
        public void OpenAPI()
        {
            host = new ServiceHost(typeof(Senor_services));
            
            host.AddServiceEndpoint(typeof(ISenor), new WSHttpBinding(), "http://127.0.0.1:3733/Senor_services");
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            {
                behavior.HttpGetEnabled = true;
                behavior.HttpGetUrl =new Uri("http://127.0.0.1:3733/Senor_services/metadata" );
                host.Description.Behaviors.Add(behavior);
            }

            host.Open();
            Console.WriteLine("API的状态{0}", host.State);
        }
        public void Close()
        {
            if(host.State==CommunicationState.Opened)
                host.Close();
        }
    }
}